package cn.tedu.eshihui.controller;

import cn.tedu.eshihui.pojo.dto.AdminAddNewDTO;
import cn.tedu.eshihui.pojo.dto.AdminLoginDTO;
import cn.tedu.eshihui.pojo.dto.AdminUpdateDTO;
import cn.tedu.eshihui.pojo.vo.AdminListItemVO;
import cn.tedu.eshihui.security.LoginPrincipal;
import cn.tedu.eshihui.service.IAdminService;

import cn.tedu.eshihui.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@Validated
@RequestMapping("/admins")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    public AdminController() {
        log.debug("创建控制器对象：AdminController");
    }


    @ApiOperation("管理员登陆")
    @ApiOperationSupport(order = 10)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO) {
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }


    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO) {
        log.debug("开始处理【添加管理员】的请求，参数：{}", adminAddNewDTO);
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("管理员列表")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @GetMapping("list")
    public JsonResult<List<AdminListItemVO>> list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【查询管理员列表】的请求，参数：无");
        log.debug("当事人：{}", loginPrincipal);
        log.debug("当事人的ID：{}", loginPrincipal.getId());
        log.debug("当事人的用户名：{}", loginPrincipal.getUsername());
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("删除管理员")
    @ApiOperationSupport(order = 300)
//    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @GetMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        adminService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改管理员")
    @ApiOperationSupport(order = 400)
//    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @GetMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, AdminUpdateDTO adminUpdateDTO) {
        adminService.updateInfoById(id, adminUpdateDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "管理员ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> setEnable(@Range(min = 1, max = 1000000, message = "启用管理员失败，管理员ID非法！")
                                      @PathVariable Long id) {
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "管理员ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> setDisable(@Range(min = 1, max = 1000000, message = "禁用管理员失败，管理员ID非法！")
                                       @PathVariable Long id) {
        log.debug("开始处理【禁用管理员】的请求，参数：{}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }
}

